package com.ruoyi.school.chapter.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.ruoyi.common.core.domain.Ztree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.school.chapter.mapper.ChapterMapper;
import com.ruoyi.school.chapter.domain.Chapter;
import com.ruoyi.school.chapter.service.IChapterService;
import com.ruoyi.common.core.text.Convert;

/**
 * 章节Service业务层处理
 * 
 * @author qiang
 * @date 2022-06-26
 */
@Service
public class ChapterServiceImpl implements IChapterService 
{
    @Autowired
    private ChapterMapper chapterMapper;

    /**
     * 查询章节
     * 
     * @param id 章节主键
     * @return 章节
     */
    @Override
    public Chapter selectChapterById(Long id)
    {
        return chapterMapper.selectChapterById(id);
    }

    /**
     * 查询章节列表
     * 
     * @param chapter 章节
     * @return 章节
     */
    @Override
    public List<Chapter> selectChapterList(Chapter chapter)
    {
        return chapterMapper.selectChapterList(chapter);
    }

    /**
     * 新增章节
     * 
     * @param chapter 章节
     * @return 结果
     */
    @Override
    public int insertChapter(Chapter chapter)
    {
        return chapterMapper.insertChapter(chapter);
    }

    /**
     * 修改章节
     * 
     * @param chapter 章节
     * @return 结果
     */
    @Override
    public int updateChapter(Chapter chapter)
    {
        return chapterMapper.updateChapter(chapter);
    }

    /**
     * 批量删除章节
     * 
     * @param ids 需要删除的章节主键
     * @return 结果
     */
    @Override
    public int deleteChapterByIds(String ids)
    {
        return chapterMapper.deleteChapterByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除章节信息
     * 
     * @param id 章节主键
     * @return 结果
     */
    @Override
    public int deleteChapterById(Long id)
    {
        return chapterMapper.deleteChapterById(id);
    }

    /**
     * 查询章节树列表
     * 
     * @return 所有章节信息
     */
    @Override
    public List<Ztree> selectChapterTree()
    {
        List<Chapter> chapterList = chapterMapper.selectChapterList(new Chapter());
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (Chapter chapter : chapterList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(chapter.getId());
            ztree.setpId(chapter.getFatherId());
            ztree.setName(chapter.getName());
            ztree.setTitle(chapter.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public List<Ztree> selectChapterTree(Long courseId)
    {
        Chapter c = new Chapter();
        c.setCourseId(courseId);
        List<Chapter> chapterList = chapterMapper.selectChapterList(c);
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (Chapter chapter : chapterList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(chapter.getId());
            ztree.setpId(chapter.getFatherId());
            ztree.setName(chapter.getName());
            ztree.setTitle(chapter.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
